package cdu.hjx.project.sys.service.impl;

import cdu.hjx.project.pojo.Dict;
import cdu.hjx.project.pojo.DictDetail;
import cdu.hjx.project.sys.mapper.DictMapper;
import cdu.hjx.project.sys.service.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    // 注入Mapper
    @Resource
    private DictMapper dictMapper;
    @Override
    public int addDict(Dict dict) throws Exception {
        return dictMapper.addDict(dict);
    }

    @Override
    public Dict findDict(String dname) throws Exception {
        return dictMapper.findDict(dname);
    }

    // 添加字典的业务层逻辑
    @Override
    public int insertDict(Dict dict) throws Exception {
        // 添加的手先判断是否存在要添加的字典
        Dict db_dict = findDict(dict.getDname());
        /**
         * 如果此处没找到数据，那么对象则没有创建成功，所以需要写成null == db_dict
         */
        // if (null == db_dict) {
        //     // 如果是空的，表示数据库中不存在要添加的字典，可以添加
        //     return addDict(dict);
        // }
        // return -1;

        return null == db_dict ? addDict(dict) : -1;
    }

    @Override
    public List<Dict> dicts() throws Exception {
        return dictMapper.dicts();
    }

    @Override
    public Dict findDictById(Integer id) throws Exception {
        return dictMapper.findDictById(id);
    }

    @Override
    public int updateDict(Dict dict) throws Exception {
        return dictMapper.updateDict(dict);
    }

    @Override
    public int deleteDict(Integer id) throws Exception {
        return dictMapper.deleteDict(id);
    }

    @Override
    public List<DictDetail> list(String dtype) throws Exception {
        return dictMapper.list(dtype);
    }
}
